// JobQDlg.cpp : implementation file
//Author : Pradeep K Sahu
//Email : sahupk@yahoo.com
//Note   : You are free to use the code, and modify. Provided you
//         don't remove this comments. It will be great if you can provide
//		   your feedback.


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#include "stdafx.h"
#include "JobQ.h"
#include "JobQDlg.h"
#include "JobType1.h"
#include "JobType2.h"
static UINT REFRESH_LIST = ::RegisterWindowMessage("REFRESH LIST");
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About


class CAboutDlg : public CDialog
{
public:

	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};


CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}


void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CJobQDlg dialog

CJobQDlg::CJobQDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CJobQDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CJobQDlg)
	m_Pause = FALSE;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}


void CJobQDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CJobQDlg)
	DDX_Control(pDX, IDC_PROGRESS_TH_COUNT, m_ThCountProgBar);
	DDX_Control(pDX, IDC_LIST1, m_ListCtrl);
	DDX_Check(pDX, IDC_PAUSE_CHK, m_Pause);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CJobQDlg, CDialog)
	//{{AFX_MSG_MAP(CJobQDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_ADD_J1, OnButtonAddJob1)
	ON_BN_CLICKED(IDC_BUTTON_ADD_J2, OnButtonAddJob2)
	ON_BN_CLICKED(IDC_BUTTON_MINUS, OnButtonMinus)
	ON_BN_CLICKED(IDC_BUTTON_PLUS, OnButtonPlus)
	ON_REGISTERED_MESSAGE(REFRESH_LIST,OnRefreshList)
	ON_BN_CLICKED(IDC_PAUSE_CHK, OnPauseChk)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CJobQDlg message handlers

BOOL CJobQDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	
	 LV_COLUMN lvcolumn;
	 lvcolumn.mask=LVCF_TEXT|LVCF_WIDTH|LVCF_SUBITEM|LVCF_FMT,
	 lvcolumn.fmt = LVCFMT_LEFT;
	 lvcolumn.iSubItem=0;
	 lvcolumn.cx=100;
	 lvcolumn.pszText="JobID";
	  this->m_ListCtrl.InsertColumn(0,&lvcolumn);
	 lvcolumn.pszText="Priority";
	 this->m_ListCtrl.InsertColumn(1,&lvcolumn);
	this->m_ThCountProgBar.SetRange(0,10);
	CJobQApp * pTheApp = (CJobQApp *)AfxGetApp(); //The application
	this->m_ThCountProgBar.SetPos(pTheApp->m_jobQ.getMaxNoOfExecuter());
	return TRUE;  // return TRUE  unless you set the focus to a control
}


void CJobQDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.


void CJobQDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CJobQDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CJobQDlg::OnButtonAddJob1()
{
	CJobQApp * pApp ;
	pApp = 	(CJobQApp * )AfxGetApp();
	CJobType1* pJob = new CJobType1();
	pApp->m_jobQ.addJob(pJob);
	Refresh() ;
}

void CJobQDlg::OnButtonAddJob2()
{
	CJobQApp * pApp ;
	pApp = 	(CJobQApp * )AfxGetApp();
	CJob* pJob;
	pJob = new CJobType2();
	pJob->setPriority(6);
	pApp->m_jobQ.addJob(pJob);
	Refresh() ;
}

/*void CJobQDlg::OnButtonRefresh()
{
}*/

void CJobQDlg::Refresh()
{
	CJobQApp * pTheApp = (CJobQApp *)AfxGetApp(); //The application
	//Initialise the ComboBox Data
	pTheApp->m_jobQ.m_cs.Lock();
	CJob *pJob=NULL;
	POSITION pos;

	pos = pTheApp->m_jobQ.m_jobQList.GetHeadPosition();
	LV_ITEM lvitem1;
	this->m_ListCtrl.DeleteAllItems();
	for (int i=0;i < pTheApp->m_jobQ.m_jobQList.GetCount() ;i++)
	{
		pJob = pTheApp->m_jobQ.m_jobQList.GetNext(pos);
		lvitem1.mask=LVIF_TEXT;
		lvitem1.iItem=i;
		lvitem1.iSubItem=0;
		char buffer[20];
	    _itoa( pJob->getID(), buffer, 10 );
		CString str = buffer; 
		lvitem1.pszText=(char *) (LPCTSTR)str;
		this->m_ListCtrl.InsertItem(&lvitem1);
		lvitem1.iSubItem=1;
		_itoa( pJob->getPriority(), buffer, 10 );
		str = buffer; 
		lvitem1.pszText=(char *) (LPCTSTR)str;
		this->m_ListCtrl.SetItem(&lvitem1);
	}//END for
	pTheApp->m_jobQ.m_cs.Unlock();
}

LRESULT CJobQDlg::OnRefreshList(WPARAM wParam, LPARAM lParam)
{
	Refresh();
	return 0;
}

void CJobQDlg::OnButtonMinus() 
{
	CJobQApp * pTheApp = (CJobQApp *)AfxGetApp(); //The application
	if(pTheApp->m_jobQ.getMaxNoOfExecuter() >2)
	{
		pTheApp->m_jobQ.setMaxNoOfExecuter(pTheApp->m_jobQ.getMaxNoOfExecuter()-1);	
	}
	else
	{
		AfxMessageBox("Already at the minimum.That is two.");
	}
	this->m_ThCountProgBar.SetPos(pTheApp->m_jobQ.getMaxNoOfExecuter());
	
}

void CJobQDlg::OnButtonPlus() 
{
	
	CJobQApp * pTheApp = (CJobQApp *)AfxGetApp(); //The application
	if(pTheApp->m_jobQ.getMaxNoOfExecuter() <10)
	{
		pTheApp->m_jobQ.setMaxNoOfExecuter(pTheApp->m_jobQ.getMaxNoOfExecuter()+1);	
	}
	else
	{
			AfxMessageBox("Already at the maximum.That is 10.");
	}
	
	this->m_ThCountProgBar.SetPos(pTheApp->m_jobQ.getMaxNoOfExecuter());

}


void CJobQDlg::OnPauseChk() 
{
	CJobQApp * pTheApp = (CJobQApp *)AfxGetApp(); //The application
	this->UpdateData();
	if(this->m_Pause == TRUE)
	{
		pTheApp->m_jobQ.pause();
	}
	else
	{
		pTheApp->m_jobQ.resume();
	}
}
